cd $firmpath 

global firstyear 1996 // 2011  
global lastyear 2010 // 2017

use transaction_all_id_conc$firstyear$lastyear.dta, clear 
sort year month 
egen date=group(year month) 
g buyer=importer
egen min=min(date), by(siren buyer iso2 id_conc) 
egen max=max(date), by(siren buyer iso2 id_conc) 

merge m:1 siren iso2 using  lifi_hq_reshape
drop if _m==2 
g hq=_m==3
drop _m 
merge m:1 siren iso2 using  lifi_aff
drop if _m==2 
g aff=_m==3
drop _m 

g intra=(hq==1 | aff==1) 
replace intra=0 if intra==.
egen _=sum(export) , by(intra)
tab intra _ 
drop if intra==1 // drop 7.7% of obs, accounting for 17% of the value of exports 
g new = date == min 
* egen test=count(export),by(siren buyer iso2 id_conc) 
egen buyer_new=nvals(buyer) if new==1, by(year month id_conc iso2) 
egen seller_new=nvals(siren) if new==1, by(year month id_conc iso2) 

sort siren buyer iso2 id_conc date 
g die=siren!=siren[_n+1]| buyer!=buyer[_n+1] | id_conc!=id_conc[_n+1]
g length=max-min 
sum length, d 


g length_interv=0 
replace length_interv=1 if length <13 & length>0
replace length_interv=2 if length>12 & length < 37  
replace length_interv=3 if length>36 

collapse (sum) export new die (mean) length buyer_new seller_new, by(month year iso2 id_conc length_interv)
count 
compress
save series_newmatch_nomne$firstyear$lastyear, replace 

use $countrypath/gdp_marketprice, clear 

g iso2="."
replace iso2="DE" if country=="Germany (until 1990 former territory of the FRG)"
replace iso2="AT" if country=="Austria"
replace iso2="BE" if country=="Belgium"
replace iso2="DK" if country=="Denmark"
replace iso2="ES" if country=="Spain"
replace iso2="FI" if country=="Finland"
replace iso2="GB" if country=="United Kingdom"
replace iso2="GR" if country=="Greece"
replace iso2="IT" if country=="Italy"
replace iso2="NL" if country=="Netherlands"
replace iso2="PT" if country=="Portugal"
replace iso2="SE" if country=="Sweden"

drop if iso2=="."

sort year trim 
egen t=group(year trim) 
egen c=group(iso2)
xtset c t 
destring gdp, replace
g gr_gdp=gdp/L4.gdp - 1
egen m=mean(gr_gdp), by(iso2) 
egen sd=sd(gr_gdp), by(iso2) 

g crisis=gr_gdp<m - sd 
drop c t date
destring year, replace 
save tmp4, replace

use series_newmatch_nomne$firstyear$lastyear, clear 
g trim=1 if month<4
replace trim=2 if month>=4 & month<7
replace trim=3 if month>=7 & month<10
replace trim=4 if month>=10
collapse (sum) new die export, by(year trim iso2 id_conc)
g insample=0
foreach i in $countrysample {
	replace insample=1 if iso2=="`i'"
}
keep if insample==1
sort iso2 id_conc year 
bys iso2 id_conc: g first=_n==1
g _=year if first==1
egen firsty=max(_), by(id_conc iso2)
fillin id_conc iso2 year trim 
replace export=0 if export==. 
sort iso2 id_conc year trim 
bys iso2 id_conc: g cum=sum(export) 

replace new=0 if new==. & cum>0 
replace die=0 if new==. & cum>0 

merge m:1 iso2 year trim using tmp4 
keep if _m==3
qui tab iso2, g(cty_)
egen cty=group(iso2) 

egen pm=group(id_conc trim) 
egen ct=group(iso2 trim year) 
egen pmt=group(id_conc trim year) 
egen pmct=group(id_conc trim year iso2)

g lnew=log(1+new) 
g ldie=log(die + 1) 

save base4reg_nomne$firstyear$lastyear, replace 

erase tmp4.dta 